<template>
  <div class="pingjia-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2>评价管理</h2>
<!--      <el-button type="primary" @click="showAddDialog">-->
<!--        <el-icon><Plus /></el-icon>-->
<!--        添加评价-->
<!--      </el-button>-->
    </div>

    <!-- 标签页 -->
    <el-tabs v-model="activeTab" @tab-change="handleTabChange" class="pingjia-tabs">
      <el-tab-pane label="待审核" name="pending">
        <el-table :data="pendingList" v-loading="loading" stripe class="data-table">
          <el-table-column type="index" label="序号" width="80" align="center" />
          <el-table-column label="用户ID" width="80" align="center" #default="scope">
            {{scope.row.user.id}}
          </el-table-column>
          <el-table-column label="用户头像" width="80" align="center" #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.user.wxHeadPic" :fit="fit" />
          </el-table-column>
          <el-table-column prop="title" label="评价内容" min-width="200" />
          <el-table-column prop="user.username" label="用户名" width="120" />
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-tag type="warning">待审核</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <el-button type="success" size="small" @click="approveEvaluation(scope.row.id)">
                <el-icon><Check /></el-icon>
                通过
              </el-button>
              <el-button type="danger" size="small" @click="rejectEvaluation(scope.row.id)">
                <el-icon><Close /></el-icon>
                拒绝
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="已通过" name="approved">
        <el-table :data="approvedList" v-loading="loading" stripe class="data-table">
          <el-table-column type="index" label="序号" width="80" align="center" />
          <el-table-column label="用户ID" width="80" align="center" #default="scope">
            {{scope.row.user.id}}
          </el-table-column>
          <el-table-column label="用户头像" width="80" align="center" #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.user.wxHeadPic" :fit="fit" />
          </el-table-column>
          <el-table-column prop="title" label="评价内容" min-width="200" />
          <el-table-column prop="user.username" label="用户名" width="120" />
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-tag type="success">已通过</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="已拒绝" name="rejected">
        <el-table :data="rejectedList" v-loading="loading" stripe class="data-table">
          <el-table-column type="index" label="序号" width="80" align="center" />
          <el-table-column label="用户ID" width="80" align="center" #default="scope">
            {{scope.row.user.id}}
          </el-table-column>
          <el-table-column label="用户头像" width="80" align="center" #default="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.user.wxHeadPic" :fit="fit" />
          </el-table-column>
          <el-table-column prop="title" label="评价内容" min-width="200" />
          <el-table-column prop="user.username" label="用户名" width="120" />
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-tag type="danger">已拒绝</el-tag>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>

    <!-- 添加评价对话框 -->
    <el-dialog v-model="addDialogVisible" title="添加评价" width="500px">
      <el-form :model="addForm" :rules="addRules" ref="addFormRef" label-width="80px">
        <el-form-item label="评价内容" prop="title">
          <el-input
            v-model="addForm.title"
            type="textarea"
            :rows="4"
            placeholder="请输入评价内容"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="addForm.userId" placeholder="请输入用户ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAdd" :loading="submitting">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Check, Close } from '@element-plus/icons-vue'
import { pingjiaList1 } from '@/assets/pingjiaList1.js'
import { pingjiaList2 } from '@/assets/pingjiaList2.js'
import { pingjiaList3 } from '@/assets/pingjiaList3.js'
import { pingjiaAdd } from '@/assets/pingjiaAdd.js'
import { pingjiaTg } from '@/assets/pingjiaTg.js'
import { pingjiaJj } from '@/assets/pingjiaJj.js'

// 响应式数据
const activeTab = ref('pending')
const loading = ref(false)
const submitting = ref(false)
const pendingList = ref([])
const approvedList = ref([])
const rejectedList = ref([])

// 添加评价相关
const addDialogVisible = ref(false)
const addFormRef = ref()
const addForm = reactive({
  title: '',
  userId: ''
})

const addRules = {
  title: [
    { required: true, message: '请输入评价内容', trigger: 'blur' },
    { min: 1, max: 500, message: '评价内容长度在 1 到 500 个字符', trigger: 'blur' }
  ],
  userId: [
    { required: true, message: '请输入用户ID', trigger: 'blur' }
  ]
}

// 生命周期钩子
onMounted(() => {
  fetchPendingList()
})

// 方法
// 切换标签页
const handleTabChange = (tab) => {
  if (tab === 'pending') {
    fetchPendingList()
  } else if (tab === 'approved') {
    fetchApprovedList()
  } else if (tab === 'rejected') {
    fetchRejectedList()
  }
}

// 获取待审核评价列表
const fetchPendingList = () => {
  loading.value = true
  pingjiaList1()
    .then(res => {
      if (res.data.code === 200) {
        pendingList.value = res.data.data || []
      } else {
        ElMessage.error('获取待审核评价列表失败')
      }
    })
    .catch(err => {
      console.error('获取待审核评价列表出错:', err)
      ElMessage.error('获取待审核评价列表出错')
    })
    .finally(() => {
      loading.value = false
    })
}

// 获取已通过评价列表
const fetchApprovedList = () => {
  loading.value = true
  pingjiaList2()
    .then(res => {
      if (res.data.code === 200) {
        approvedList.value = res.data.data || []
      } else {
        ElMessage.error('获取已通过评价列表失败')
      }
    })
    .catch(err => {
      console.error('获取已通过评价列表出错:', err)
      ElMessage.error('获取已通过评价列表出错')
    })
    .finally(() => {
      loading.value = false
    })
}

// 获取已拒绝评价列表
const fetchRejectedList = () => {
  loading.value = true
  pingjiaList3()
    .then(res => {
      if (res.data.code === 200) {
        rejectedList.value = res.data.data || []
      } else {
        ElMessage.error('获取已拒绝评价列表失败')
      }
    })
    .catch(err => {
      console.error('获取已拒绝评价列表出错:', err)
      ElMessage.error('获取已拒绝评价列表出错')
    })
    .finally(() => {
      loading.value = false
    })
}

// 显示添加评价对话框
const showAddDialog = () => {
  addDialogVisible.value = true
  // 重置表单
  if (addFormRef.value) {
    addFormRef.value.resetFields()
  }
}

// 提交添加评价
const submitAdd = () => {
  addFormRef.value.validate((valid) => {
    if (valid) {
      submitting.value = true
      pingjiaAdd(addForm)
        .then(res => {
          if (res.data.code === 200) {
            ElMessage.success('添加评价成功')
            addDialogVisible.value = false
            fetchPendingList() // 刷新待审核列表
          } else {
            ElMessage.error('添加评价失败')
          }
        })
        .catch(err => {
          console.error('添加评价出错:', err)
          ElMessage.error('添加评价出错')
        })
        .finally(() => {
          submitting.value = false
        })
    }
  })
}

// 通过评价
const approveEvaluation = (id) => {
  ElMessageBox.confirm('确定要通过这条评价吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    loading.value = true
    pingjiaTg(id)
      .then(res => {
        if (res.data.code === 200) {
          ElMessage.success('评价已通过')
          fetchPendingList() // 刷新待审核列表
        } else {
          ElMessage.error('操作失败')
        }
      })
      .catch(err => {
        console.error('通过评价出错:', err)
        ElMessage.error('通过评价出错')
      })
      .finally(() => {
        loading.value = false
      })
  }).catch(() => {
    // 用户取消操作
  })
}

// 拒绝评价
const rejectEvaluation = (id) => {
  ElMessageBox.confirm('确定要拒绝这条评价吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    loading.value = true
    pingjiaJj(id)
      .then(res => {
        if (res.data.code === 200) {
          ElMessage.success('评价已拒绝')
          fetchPendingList() // 刷新待审核列表
        } else {
          ElMessage.error('操作失败')
        }
      })
      .catch(err => {
        console.error('拒绝评价出错:', err)
        ElMessage.error('拒绝评价出错')
      })
      .finally(() => {
        loading.value = false
      })
  }).catch(() => {
    // 用户取消操作
  })
}
</script>

<style scoped>
.pingjia-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.pingjia-tabs {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.data-table {
  width: 100%;
  margin-top: 20px;
}

.data-table .el-table__header {
  background-color: #f8f9fa;
}

.data-table .el-table__header th {
  background-color: #f8f9fa !important;
  color: #333;
  font-weight: 600;
}

.data-table .el-table__row:hover {
  background-color: #f5f7fa;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 标签样式 */
.el-tag {
  font-weight: 500;
}

/* 按钮样式 */
.el-button {
  margin: 0 2px;
}

.el-button--success {
  background-color: #67c23a;
  border-color: #67c23a;
}

.el-button--success:hover {
  background-color: #85ce61;
  border-color: #85ce61;
}

.el-button--danger {
  background-color: #f56c6c;
  border-color: #f56c6c;
}

.el-button--danger:hover {
  background-color: #f78989;
  border-color: #f78989;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .pingjia-container {
    padding: 10px;
  }

  .page-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }

  .data-table {
    font-size: 14px;
  }
}
</style>